<%@ page language="java" contentType="text/html; charset=utf-8"   pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>产品列表 </title>
</head>
<script src="${pageContext.request.contextPath}/js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
function checkAll(form, checked){
    for (var i=0; i<form.elements.length; i++) {
        var e = form.elements[i];
        if (e.type=="checkbox"){
			if(e.checked != checked){
				e.click();
			}
			// e.checked = checked;
		}
    }
}

function deleteAll(form){
	var idStr = "";
	for (var i=0; i<form.elements.length; i++) {
        var e = form.elements[i];
        if (e.type=="checkbox" && e.name == 'checkOne'){
			if(e.checked){
				idStr += e.value + ',';
			}			
		}
    }
    
    if(idStr == ""){
    	alert("请选择要删除的项！");
    	return false;
    }
    
   if(confirm("确认删除选中项？")){
    	window.document.location = "${pageContext.request.contextPath}/admin/product-list!delete.do?spec=" + idStr;
    }
    
}

function deleteOne(idStr){
	if(confirm("确认删除该项？")){
    	window.document.location = "${pageContext.request.contextPath}/admin/product-list!delete.do?spec=" + idStr;
    }
}


function updateOnline(form,k){
	var idStr = "";
	for (var i=0; i<form.elements.length; i++) {
        var e = form.elements[i];
        if (e.type=="checkbox" && e.name == 'checkOne'){
			if(e.checked){
				idStr += e.value + ',';
			}			
		}
    }
    
    if(idStr == ""){
    	alert("请选择要设置的项！");
    	return false;
    }

    var url = "${pageContext.request.contextPath}/admin/product-list!updateOnlie.do?idStr=" + idStr + "&value=" + k;
	$.ajax({
		url: url,
		dataType: "text",
		type: "post",
		success: function(data){
			alert("设置成功，共修改" + data + "条记录！");
			document.forms[0].submit();
		},
		error: function(){
			alert("设置失败！");
		}
	});
}

</script>
<body>
<s:form id="form1" action="product-list!list.do" theme="simple" method="post">
<table border="0" width="100%">
	<tr>
		<td width="100%" align="center">
			产品列表 
		</td>
	</tr>
</table>
<table width="100%">
	<tr>
		<td colspan="9">
			产品名称：<s:textfield id="name" name="name"/>
			&nbsp;&nbsp;
			
			系列：<s:select label="系列" 
						id="seriesId"
						name="seriesId" 
						headerKey="-1"
						headerValue="请选择系列"
						list="seriesList" 
						listKey="id" 
						listValue="value"/>
			&nbsp;&nbsp;
			品名：<s:select label="品名" 
					id="seriesNameId"
					name="seriesNameId" 
					headerKey="-1"
					headerValue="请选择品名"
					list="seriesNameList" 
					listKey="id" 
					listValue="value"/>
			&nbsp;&nbsp;
			上下架：<s:select label="上下架标记"
					       id="online"
					       name="online"
					       headerKey="-1" headerValue="请选择上下架标记"
					       list="#{'1':'上架', '2':'下架'}"
						   required="true"/>
			&nbsp;&nbsp;			   
			<input type="submit" value="查询">
			&nbsp;&nbsp;
			<input type="button" value="设置上架" onclick="updateOnline(this.form,1);"/>
			&nbsp;&nbsp;
			<input type="button" value="设置下架" onclick="updateOnline(this.form,2);"/>
			&nbsp;&nbsp;&nbsp;
			<input type="button" value="删除选中项" onclick="deleteAll(this.form);"/>
		</td>
	</tr>
</table>
<table border="1" width="100%" cellpadding="0" cellspacing="0">
	<tr>
		<td width="3px" align="center">
			<input type="checkbox" id="allbox" name="allbox" onclick="checkAll(this.form,this.checked)"/>
		</td>
		<td>编号</td>
		<td>产品名称</td>
		<td>系列</td>
		<td>品名</td>
		<td>上下架标记</td>
		<td>销售单价(元/盒)</td>
		<td>规格</td>
		<td>操作</td>
	</tr>
	<s:iterator value="productList">
		<tr>
			<td align="center" width="3px" ><input type="checkbox" name="checkOne" value="<s:property value='id'/>"></td>
			<td><s:property value="no"/></td>
			<td><a href="${pageContext.request.contextPath}/product-view!input.do?id=<s:property value='id'/>" title="查看产品" target="_blank"><s:property value="name"/></a></td>
			<td><s:property value="series.value"/></td>
			<td><s:property value="seriesName.value"/></td>
			<td>
					<s:if test="%{online eq 1}">
						上架
					</s:if>
					<s:if test="%{online == 2}">
						下架
					</s:if>
			</td>
			<td><s:property value="price"/></td>
			<td><s:property value="spec"/></td>
			<td><a href="product-input!input.do?id=<s:property value='id'/>">修改</a>&nbsp;&nbsp;<a href="#" onclick="deleteOne('<s:property value="id"/>')">删除</a></td>
		</tr>
	</s:iterator>
</table>
</s:form>
</body>
</html>